package com.trainingcloud.action;

import com.trainingcloud.Globals;
import com.trainingcloud.dao.UserDAO;
import com.trainingcloud.entity.User;
import com.trainingcloud.exception.DAOException;
import com.trainingcloud.exception.SystemException;

public abstract class BaseSessionAction extends BaseAction 
{
	private String token;
	protected User currentUser;
	
	public BaseSessionAction() throws SystemException 
	{
		super();
	}

	@Override
	public String call() throws DAOException, SystemException 
	{
		int userId = Globals.getTokenManager().checkToken(token);
		if(userId<=0)
		{
			return "Invalid token.";
		}
		UserDAO userDAO = new UserDAO();
		currentUser = userDAO.getUserById(userId);
		if(currentUser==null)
		{
			return "Invalid user id.";
		}
		return callWithSession();
	}
	
	abstract public String callWithSession() throws DAOException, SystemException;

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}
}
